package us.mmapp.providers;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Users database access helper class.
 * 
 *
 */
public class UsersDbManager {
	public static final String KEY_ID = "_id";
	public static final String KEY_USERNAME = "username";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_EMAIL = "email";
	public static final String TAG = "UsersDBManager";
	private static int DATABASE_VERSION = 1;
	private static String DATABASE_NAME = "mmapp"; 

	
	private final Context mContext;
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL("CREATE TABLE users ("
					+ KEY_ID + " INTEGER PRIMARY KEY, "
					+ KEY_USERNAME + " TEXT, "
					+ KEY_PASSWORD + " TEXT, "
					+ KEY_EMAIL + " TEXT);");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
					+ newVersion);
			db.execSQL("DROP TABLE IF EXISTS users");
			onCreate(db);
		}
	}
	
	/**
	 * Constructor
	 * @param context the context used to open/close a db
	 */
	public UsersDbManager(Context context)
	{
		this.mContext = context;
	}
	protected void finalize()
	{
		if (mDb != null)
			mDb.close();
	}
	/**
	 * Open the users database.
	 * @return this
	 * @throws SQLException
	 */
	public UsersDbManager open() throws SQLException
	{
		mDbHelper = new DatabaseHelper(this.mContext);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close() throws SQLException
	{
		if (mDb != null)
			mDb.close();
	}
	// Projection for fetchUser
	private static final String[] projectionUser = {
		KEY_ID,
		KEY_USERNAME,
		KEY_PASSWORD,
		KEY_EMAIL
		};
	
	/**
	 * Fetch the database for a given user.
	 * @param uid
	 * @return
	 * @throws SQLException
	 */
	public Cursor fetchUser(long uid) throws SQLException
	{
		Cursor cursor =
			mDb.query(true, "users", projectionUser, KEY_ID + "=" + uid,
					null, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	
	public Cursor fetchUser(String username, String pass) throws SQLException
	{
		Cursor cursor =
			mDb.query(true, "users", projectionUser, KEY_USERNAME + "='" + username + "' AND "
					+ KEY_PASSWORD + "='" + pass + "'",
					null, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	public Cursor fetchUser(String username) throws SQLException
	{
		Cursor cursor =
			mDb.query(true, "users", projectionUser, KEY_USERNAME + "='" + username + "'",
					null, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	public boolean updateUser(long uid, String user, String pass, String email)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_EMAIL, email);
		args.put(KEY_PASSWORD, pass);
		args.put(KEY_USERNAME, user);
		return mDb.update("users", args, KEY_ID + "=" + uid, null) > 0;
	}
	public long createUser(long uid, String user, String pass, String email)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_ID, uid);
		args.put(KEY_EMAIL, email);
		args.put(KEY_PASSWORD, pass);
		args.put(KEY_USERNAME, user);
		return mDb.insert("users", null, args);
		
	}
}
